//
//  ClassifyListViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/7/18.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "ClassifyListViewController.h"
#import "ClassifyItemCell.h"
#import "ClassifyFirstModel.h"
#import "ClassifySecondModel.h"
#import "SearchCacheViewController.h"   // 跳转
#import "CategoryFirstItemCell.h"
#import "CategoryGoodsViewController.h" // 二级页面

@interface ClassifyListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *firstArr;
@property (nonatomic, strong) NSMutableDictionary *secondDic;
@property (nonatomic, assign) NSInteger index;

@end

@implementation ClassifyListViewController
#pragma mark - viewController
- (instancetype)init {
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
        _firstArr = [NSMutableArray array];
        _secondDic = [NSMutableDictionary dictionary];
        _index = 0;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"分类";
    self.view.backgroundColor = [UIColor whiteColor];
    [self configureUI];
    [self loadNetWork];
}

- (void)configureUI {
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
        make.bottom.top.equalTo(self.view);
    }];
}

- (void)reloadUI {
    [self.collectionView reloadData];
}

- (NSMutableArray *)getFirstStr:(NSArray *)arr {
    NSMutableArray *firstArr = [NSMutableArray array];
    for (ClassifyFirstModel *model in self.firstArr) {
        [firstArr addObject:model.gcname];
    }
    return firstArr;
}

#pragma mark - NetWork
- (void)loadNetWork {
    [self getFirstClass];
}

- (void)getFirstClass {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    
    __weak typeof(self) weakSelf = self;
    [MYAFNetworking POST:URL_Get_Goodsclass parameters:parameters success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        NSDictionary *responseDic = responseObject;
        NSString *code = [responseDic safeStringForKey:@"code"];
        if (code && [code isEqualToString:@"200"]) {
            NSArray *data = [responseDic safeJsonObjectForKey:@"data"];
            [weakSelf.firstArr removeAllObjects];
            for (NSDictionary *modelDic in data) {
                ClassifyFirstModel *model = [ClassifyFirstModel mj_objectWithKeyValues:modelDic];
                [weakSelf.firstArr addObject:model];
            }
            [weakSelf reloadUI];
        } else {
            [MBProgressHUD showError:weakSelf.view title:[responseDic safeStringForKey:@"message"] code:nil animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        [MBProgressHUD showError:weakSelf.view title:@"" code:nil animated:YES];
    } isNeedCache:NO];
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.firstArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CategoryFirstItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryFirstItemCell" forIndexPath:indexPath];
    cell.cellModel = self.firstArr[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassifyFirstModel *Model = self.firstArr[indexPath.item];
    CategoryGoodsViewController *GoodsViewController = [[CategoryGoodsViewController alloc] init];
    GoodsViewController.Model = Model;
    [self.navigationController pushViewController:GoodsViewController animated:true];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((ScreenWidth - 40)/2, 132 * ScreenWidth/375);
}

#pragma mark - Getters
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 13;
        flowLayout.minimumLineSpacing = 12 ;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; // 纵向
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.alwaysBounceHorizontal = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        [_collectionView registerNib:[UINib nibWithNibName:@"CategoryFirstItemCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CategoryFirstItemCell"];
    }
    return _collectionView;
}

@end
